package com.chb.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chb.app.feign.UserFeign;
import com.chb.app.mapper.HealthNewsMapper;
import com.chb.app.service.HealthNewsService;
import com.chb.app.vo.NewsParams;
import com.hos.entity.hos.HealthNews;
import com.hos.entity.sys.User;
import com.hos.utils.IdGeneratorUtil;
import com.hos.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author ：WuBai-T
 * @date ：Created in 2023/12/22 12:01
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class HealthNewsServiceImpl implements HealthNewsService {

    @Autowired
    private HealthNewsMapper healthNewsMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private UserFeign userFeign;


    @Override
    public List<HealthNews> getLittleNews() {
        QueryWrapper<HealthNews> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("RAND()");
        wrapper.last("LIMIT 5");
        List<HealthNews> healthNews = healthNewsMapper.selectList(wrapper);

        return healthNews;
    }

    @Override
    public List<HealthNews> getAllNews() {
        List<HealthNews> healthNews = healthNewsMapper.selectList(null);
        return healthNews;
    }

    @Override
    public HealthNews getNewsByNewsId(String newsId) {

        HealthNews healthNews = healthNewsMapper.selectById(newsId);
        return healthNews;
    }

    @Override
    public IPage<HealthNews> getNewsByParams(NewsParams params) {
        IPage<HealthNews> page = new Page<>(params.getPage(), params.getPageSize());

        LambdaQueryWrapper<HealthNews> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(params.getNewsSource())) {
            wrapper.like(HealthNews::getNewsSource, params.getNewsSource());
        }

        if (Objects.nonNull(params.getStartTime())) {
            wrapper.ge(HealthNews::getCreateTime, params.getStartTime())
                    .le(HealthNews::getCreateTime, params.getEndTime());
        }
        return this.healthNewsMapper.selectPage(page, wrapper);
    }

    @Override
    public void insertNews(HealthNews healthNews) {
        healthNews.setNewsId(IdGeneratorUtil.nextId("NEW"));
        healthNews.setCreateTime(LocalDateTime.now());
        healthNews.setCreateBy(getUserByToken().getUserName());
        healthNews.setUpdateTime(LocalDateTime.now());
        this.healthNewsMapper.insert(healthNews);
    }

    @Override
    public void updateNews(HealthNews healthNews) {
        healthNews.setUpdateTime(LocalDateTime.now());
        this.healthNewsMapper.updateById(healthNews);
    }

    @Override
    public void delNews(String newsId) {
        this.healthNewsMapper.deleteById(newsId);
    }

    /**
     * @return com.hos.entity.sys.User
     * @create by: 陈海彬
     * @description: 获取请求用户的信息
     * @create time: 2023/12/12 10:39
     */
    private User getUserByToken() {
        String token = WebUtil.getToken();
        String phone = redisTemplate.opsForValue().get("user:" + token);
        // 判断是否为空
        if (StringUtils.hasText(phone)) {
            // 更新过期时间
            redisTemplate.expire("user:" + token, 30, TimeUnit.MINUTES);
        }
        return userFeign.selectUserByPhone(phone);
    }


}